package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.tribes.group.Response;

import com.fk.dao.impl.ChanPinFenLeiDaoImpl;
import com.fk.dao.impl.ShangPinFenLeiDaoImpl;
import com.fk.entity.ChanPinFenLei;
import com.fk.entity.ShangPinFenLei;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;


public class AdminProductCategoryServlet extends BaseServlet {
	/**
	 * localhost:8080/wuyijiu/adminProductCategory?reqName=getProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		List<ChanPinFenLei> cpList=cpDao.getChanFenLei();//查到的所有产品分类集合
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		String res=gson.toJson(cpList);
		out.print(res);
		out.flush();
		out.close();
	}
	
	public void delProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int cId = Integer.parseInt(request.getParameter("categoryId"));
		
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		int c = cpDao.deleteProductCateById(cId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(c>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();
	}
	/**
	 * 添加产品分类
	 * localhost:8080/wuyijiu/adminProductCategory?reqName=addProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinFenLeiDaoImpl pcDao=new ChanPinFenLeiDaoImpl();
		
		String jiJie=request.getParameter("jiJie");
		if (jiJie!=null && !jiJie.equals("")) {
			List<ChanPinFenLei> pcList=pcDao.getProductCategoryByName(jiJie);
			if (pcList.size()==0) {//没查到
				ChanPinFenLei pc=new ChanPinFenLei(0, jiJie, "");
				int n=pcDao.addProductCategory(pc);
				
				if (n>0) {//添加成功
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "分类名称已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	/**
	 * ��Ӳ�Ʒ����
	 * localhost:8080/wuyijiu/adminProductCategory?reqName=getChanpinfenleiById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanpinfenleiById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		ChanPinFenLeiDaoImpl pcDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		int id = Integer.parseInt(request.getParameter("cId"));//接收前台传来的参数，并转化为int类型
		ChanPinFenLei pc = pcDao.getChanpinfenleiById(id);//调用查询方法查到的结果
		
		Gson gson = new Gson();//创建gson
		String res = gson.toJson(pc);//把产品分类pc转化为json格式字符串
		
		out.print(res);//响应数据
		
		out.flush();//清空
		out.close();//关闭
	}
	/**
	 * ��Ӳ�Ʒ����
	 * localhost:8080/wuyijiu/adminProductCategory?reqName=updateChanpinfenleiById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateChanpinfenleiById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		ChanPinFenLeiDaoImpl pcDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		int fenleiId = Integer.parseInt(request.getParameter("fenleiId"));
		String jiJie = request.getParameter("jijie");
		
		List<ChanPinFenLei> pcList = pcDao.getProductCategoryByName(jiJie);
		if (pcList.size() == 0) {
			ChanPinFenLei pc = new ChanPinFenLei(fenleiId,jiJie);
			int n = pcDao.updateChanpinfenlei(pc);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else {
			out.print(Result.toClient(false, "分类名已存在", ""));
		}
		
		out.flush();//清空
		out.close();//关闭
	}
}
